/*
 * AutoAnswer
 * Copyright (C) 2010 EverySoft
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.everysoft.autoanswer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

public class AutoAnswerReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {

		// Load preferences
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

		// Check phone state
		String phone_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
		// Let the phone ring for a set delay
		try {
			Thread.sleep(Integer.parseInt(prefs.getString("delay", "2")) * 1000);
		} catch (InterruptedException e) {
			// We don't really care
		}

		if (phone_state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
			// showScreenButton(context, 200, "Screenss");
			final Intent newIntent = new Intent(context, ScreenButtonActivity.class);
			newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(newIntent);
			// showScreenButton(context, 10, "Screen");
		} else {
			final Intent newIntent = new Intent();
			newIntent.setAction("com.finish.ACTIVITY");
			context.sendBroadcast(newIntent);
		}
		// else if (phone_state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
		// final Intent newIntent = new Intent(context,
		// OnCallButtonActivity.class);
		// newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// context.startActivity(newIntent);
		// }
		// if (phone_state.equals(TelephonyManager.EXTRA_STATE_RINGING) &&
		// prefs.getBoolean("enabled", false)) {
		// // Check for "second call" restriction
		// if (prefs.getBoolean("no_second_call", false)) {
		// AudioManager am = (AudioManager)
		// context.getSystemService(Context.AUDIO_SERVICE);
		// if (am.getMode() == AudioManager.MODE_IN_CALL) {
		// return;
		// }
		// }
		//
		// // Check for contact restrictions
		// String which_contacts = prefs.getString("which_contacts", "all");
		// if (!which_contacts.equals("all")) {
		// int is_starred = isStarred(context, number);
		// if (which_contacts.equals("contacts") && is_starred < 0) {
		// return;
		// }
		// else if (which_contacts.equals("starred") && is_starred < 1) {
		// return;
		// }
		// }
		//
		// // Call a service, since this could take a few seconds
		// context.startService(new Intent(context,
		// AutoAnswerIntentService.class));
		// }
	}

	public void showScreenButton(Context c, int xLength, String buttonName) {
		// check if pref is ok with invite in call
		// if(!Preferences.getInstance(c.getInviteInCall())){return ; }
		// sets the WindowManager
		WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);

		WindowManager.LayoutParams params = new WindowManager.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
						| WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
				WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
						| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
		params.x = xLength;
		params.height = LayoutParams.WRAP_CONTENT;
		params.width = LayoutParams.WRAP_CONTENT;
		params.format = PixelFormat.TRANSLUCENT;
		final Context ct = c;

		params.gravity = Gravity.TOP;
		params.setTitle(buttonName);

		LinearLayout ly = new LinearLayout(c);
		ly.setOrientation(LinearLayout.VERTICAL);

		Button screenButton = new Button(c);
		screenButton.setClickable(true);
		screenButton.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.icon));
		screenButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				System.out.println("Clicked.....");
				Intent service = new Intent(ct, AutoAnswerIntentService.class);
				ct.startService(service);
				// Toast.makeText(v.getContext(), "adding to blacklist..",
				// Toast.LENGTH_LONG).show();
				// v.setClickable(false);
			}
		});
	}

	// returns -1 if not in contact list, 0 if not starred, 1 if starred
	private int isStarred(Context context, String number) {
		int starred = -1;
		Cursor c = context.getContentResolver().query(
				Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number),
				new String[] { PhoneLookup.STARRED }, null, null, null);
		if (c != null) {
			if (c.moveToFirst()) {
				starred = c.getInt(0);
			}
			c.close();
		}
		return starred;
	}
}
